package seotools.positions.direct;

import data.DataHelper;
import data.obj.Repair;
import jxl.read.biff.BiffException;
import mat.xls.ExcelParser;
import obj.Item;

import java.io.*;
import java.util.List;
import java.util.Map;

public class SiteHelper {
    public static void main(String[] args) throws IOException, BiffException {
//        PrintStream out = new PrintStream(new FileOutputStream("motorola.table.txt"), false, "UTF-8");
//        StringBuilder sb = new StringBuilder("<table style=\"font-size:12px\">\n");
//        BufferedReader reader = new BufferedReader(new FileReader("data/accum.motorola.txt"));
//        String line;
//        List<String> models = new ArrayList<String>();
//        while ((line = reader.readLine()) != null) {
//            Accum accum = Accum.parseAccum(line, 1);
//            models.addAll(Arrays.asList(accum.devices));
//        }
//        sb.append(modelsToTable(models.toArray(new String[0]), "ремонт Motorola", 5));
//        sb.append("</table>\n");
//        out.println(sb);
//        PrintStream out = new PrintStream(new FileOutputStream("table.txt"), false, "UTF-8");
//        List<Repair> phones = ExcelParser.parseRepair("data/extramobi/remont/remont.xls", 200);
//        for (Repair phone : phones) {
//            StringBuilder sb = new StringBuilder("<table style=\"font-size:12px\">\n");
//            sb.append(modelsToTable(phone.models.toArray(new String[0]), "ремонт " + phone.vendor, 5, "http://extramobi.ru/product/remont_" + DataHelper.codeInURL(phone.vendor)));
//            sb.append("</table>\n");
//            out.println(sb);
//        }

        String path = "data/extramobi/remont/remont.xls";
        Map<String, List<Item>> itemsByBrand = Item.parseRemontItemsByBrand(path);
        for (String brand : itemsByBrand.keySet()) {
            List<Item> items = itemsByBrand.get(brand);
//            for (Item item : items) {
                StringBuilder sb = new StringBuilder("<table style=\"font-size:12px\">\n");

                sb.append(modelsToTable(items, "ремонт ", 5, "http://extramobi.ru/product/remont"));
                sb.append("</table>\n");
                System.out.println(sb);
//            }
        }
    }

    public static String modelsToTable(List<Item> items, String prefix, int columns, String baseUrl) {
        StringBuilder sb = new StringBuilder();
        for (int i = 1; i <= items.size(); i++) {
            if (i == 1 || i % columns + 1 == 0) {
                sb.append("<tr>\n");
            }
            Item item = items.get(i - 1);
            if (baseUrl != null) {
                String url = baseUrl + "_" + DataHelper.codeInURL(item.name);
                sb.append("<td width=\"150\"><strong><a href=\"").append(url).append("\">").append(prefix + item.name).append("</a></strong></td>\n");
            } else {
                sb.append("<td width=\"150\"><strong>").append(prefix + item.name).append("</strong></td>\n");
            }
            if (i % columns == 0) {
                sb.append("</tr>\n");
            }
        }
        if (sb.toString().endsWith("</td>\n")) {
            sb.append("</tr>\n");
        }
        return sb.toString();
     
    }
    public static String modelsToTable(String[] models, String name, int columns, String baseUrl) {
        StringBuilder sb = new StringBuilder();
        for (int i = 1; i <= models.length; i++) {
            if (i == 1 && i % columns + 1 == 0) {
                sb.append("<tr>\n");
            }
            if (baseUrl != null) {
                String url = baseUrl + "_" + DataHelper.codeInURL(models[i - 1]);
                sb.append("<td width=\"150\"><strong><a href=\"").append(url).append("\">").append(name).append(" ").append(models[i - 1]).append("</a></strong></td>\n");
            } else {
                sb.append("<td width=\"150\"><strong>").append(name).append(" ").append(models[i - 1]).append("</strong></td>\n");
            }
            if (i % columns == 0) {
                sb.append("</tr>\n");
            }
        }
        if (sb.toString().endsWith("</td>\n")) {
            sb.append("</tr>\n");
        }
        return sb.toString();
    }

}
